package com.douqu.game.core.entity.sprite;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.database.model.PlayerModel;
import com.douqu.game.core.entity.Grade;
import com.douqu.game.core.entity.WorldInfo;
import com.douqu.game.core.entity.ext.*;
//import com.douqu.game.core.entity.ext.InstanceInfo;
import com.douqu.game.core.factory.DataFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by bean on 2017/7/14.
 */
public class Player extends Sprite {

    public int camp;

    public boolean isDel;

    /** 当前段位 */
    public int grade;

    /** 当前段位星级 */
    public int star;

    /***/
    public int money;

    private Map<String, ExtInfo> extInfos;

    public Player(WorldInfo worldInfo)
    {
        super();

        this.grade = 1;

        extInfos = new HashMap<String, ExtInfo>();
        addExtInfo(new BagInfo(this, worldInfo));
        addExtInfo(new BattleInfo(this, worldInfo));
        addExtInfo(new InstanceInfo(this, worldInfo));

        updateLv();
    }

    public void init()
    {
        for(String key : extInfos.keySet())
        {
            extInfos.get(key).init();
        }
        getExtInfo(BattleInfo.class).init();
        getExtInfo(InstanceInfo.class).init();
    }


    public void load(PlayerModel playerModel)
    {
        if(playerModel == null)
            return;

        this.id = playerModel.getId();
        this.objectIndex = playerModel.getObjectIndex();
        this.avatar = playerModel.getAvatar();
        this.uid = playerModel.getUid();
        this.name = playerModel.getName();
        this.camp = playerModel.getCamp();
        this.lv = playerModel.getLevel();
        this.exp = playerModel.getExp();
        this.grade = playerModel.getGrade();
        this.star = playerModel.getStar();
        this.money = playerModel.getMoney();

        //这里加载数据库的数据
        byte[] byteData = playerModel.getBagInfo();
        if(byteData != null)
        {
            getExtInfo(BagInfo.class).loadFrom(new ByteBuffer(byteData));
        }

        byteData = playerModel.getBattleInfo();
        if(byteData != null)
        {
            getExtInfo(BattleInfo.class).loadFrom(new ByteBuffer(byteData));
        }
        else
        {
            getExtInfo(BattleInfo.class).init();
        }

        byteData = playerModel.getInstanceInfo();
        if(byteData != null)
        {
            getExtInfo(InstanceInfo.class).loadFrom(new ByteBuffer(byteData));
        }
        else
        {
            getExtInfo(InstanceInfo.class).init();
        }

        byteData = playerModel.getArenaInfo();
        if(byteData != null)
        {
            getExtInfo(ArenaInfo.class).loadFrom(new ByteBuffer(byteData));
        }
        else
        {
            getExtInfo(ArenaInfo.class).init();
        }
    }


    public void copyFrom(Player player)
    {
        //TODO 复制数据
    }





    public void updateLv()
    {
        this.lv = DataFactory.getInstance().getPlayerLv(exp);
    }


    /**
     * 获取星星
     * @param value
     */
    public void addStar(int value)
    {
        if(value == 0)
            return;

        star += value;
        star = star < 0 ? 0 : star;

        Grade gradeObj = null;
        if(value > 0)
        {
            DataFactory dataFactory = DataFactory.getInstance();
            for(int i = grade; i <= dataFactory.MAX_GRADE; i++)
            {
                gradeObj = dataFactory.getGameObject(DataFactory.GRADE_KEY, i);
                if(gradeObj != null)
                {
                    if(star > gradeObj.star)
                    {
                        //升级
                        grade = i;
                    }
                }
            }
        }
        else
        {
            if(grade == 1)
                return;

            for(int i = grade; i > 0; i--)
            {
                gradeObj = DataFactory.getInstance().getGameObject(DataFactory.GRADE_KEY, i);
                if(gradeObj != null)
                {
                    if(star < gradeObj.star)
                    {
                        //降级
                        grade = i;
                    }
                }
            }
        }
    }


    /**
     * 获取属性增加
     * @param attrId
     * @return
     */
    public int getAttribute(int attrId)
    {
        return 0;
    }

    /**
     * 获取对应的属性加成比例
     * @return 如果是加成50%则是返回50
     */
    public int getAttributePlus(int attrId)
    {
        return 0;
    }

    /**
     * 获取总战力
     * @return
     */
    public int getFC()
    {
        return 0;
    }

    /**
     * 添加资源
     * @param key
     * @param value
     */
    public void addAsset(int key, int value)
    {
        BagInfo bagInfo = (BagInfo) getExtInfo(BagInfo.class);
        bagInfo.addAsset(key, value);
    }

    /**
     * 获取资源数量
     * @param key
     * @return
     */
    public int getAsset(int key)
    {
        BagInfo bagInfo = (BagInfo) getExtInfo(BagInfo.class);
        return bagInfo.getAsset(key);
    }



    public void addExtInfo(ExtInfo extInfo)
    {
        if(extInfos == null)
            extInfos = new HashMap<String, ExtInfo>();

        extInfos.put(extInfo.getName(), extInfo);
    }


    public ExtInfo getExtInfo(Class cls)
    {
        return extInfos.get(cls.getSimpleName());
    }

    @Override
    public String toString() {
        return "Player{" +
                "camp=" + camp +
                ", grade=" + grade +
                ", money=" + money +
                ", star=" + star +
                ", lv=" + lv +
                "} " + super.toString();
    }
}
